1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.lang3;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertFalse;
21  import static org.junit.Assert.assertNotNull;
22  import static org.junit.Assert.assertTrue;
23  import static org.junit.Assert.fail;
24  
25  import java.io.FileInputStream;
26  import java.io.IOException;
27  import java.io.StringWriter;
28  import java.lang.reflect.Constructor;
29  import java.lang.reflect.Modifier;
30  import java.nio.charset.Charset;
31  
32  import org.apache.commons.io.IOUtils;
33  import org.apache.commons.lang3.text.translate.CharSequenceTranslator;
34  import org.apache.commons.lang3.text.translate.NumericEntityEscaper;
35  import org.junit.Test;
36  
37  /**
38   * Unit tests for {@link StringEscapeUtils}.
39   *
40   * @version $Id$
41   */
42  public class StringEscapeUtilsTest {
43      private final static String FOO = "foo";
44  
45      @Test
46      public void testConstructor() {
47          assertNotNull(new StringEscapeUtils());
48          final Constructor<?>[] cons = StringEscapeUtils.class.getDeclaredConstructors();
49          assertEquals(1, cons.length);
50          assertTrue(Modifier.isPublic(cons[0].getModifiers()));
51          assertTrue(Modifier.isPublic(StringEscapeUtils.class.getModifiers()));
52          assertFalse(Modifier.isFinal(StringEscapeUtils.class.getModifiers()));
53      }
54      
55      @Test
56      public void testEscapeJava() throws IOException {
57          assertEquals(null, StringEscapeUtils.escapeJava(null));
58          try {
59              StringEscapeUtils.ESCAPE_JAVA.translate(null, null);
60              fail();
61          } catch (final IOException ex) {
62              fail();
63          } catch (final IllegalArgumentException ex) {
64          }
65          try {
66              StringEscapeUtils.ESCAPE_JAVA.translate("", null);
67              fail();
68          } catch (final IOException ex) {
69              fail();
70          } catch (final IllegalArgumentException ex) {
71          }
72          
73          assertEscapeJava("empty string", "", "");
74          assertEscapeJava(FOO, FOO);
75          assertEscapeJava("tab", "\\t", "\t");
76          assertEscapeJava("backslash", "\\\\", "\\");
77          assertEscapeJava("single quote should not be escaped", "'", "'");
78          assertEscapeJava("\\\\\\b\\t\\r", "\\\b\t\r");
79          assertEscapeJava("\\u1234", "\u1234");
80          assertEscapeJava("\\u0234", "\u0234");
81          assertEscapeJava("\\u00EF", "\u00ef");
82          assertEscapeJava("\\u0001", "\u0001");
83          assertEscapeJava("Should use capitalized Unicode hex", "\\uABCD", "\uabcd");
84  
85          assertEscapeJava("He didn't say, \\\"stop!\\\"",
86                  "He didn't say, \"stop!\"");
87          assertEscapeJava("non-breaking space", "This space is non-breaking:" + "\\u00A0",
88                  "This space is non-breaking:\u00a0");
89          assertEscapeJava("\\uABCD\\u1234\\u012C",
90                  "\uABCD\u1234\u012C");
91      }
92  
93      /**
94       * Tests https://issues.apache.org/jira/browse/LANG-421
95       */
96      @Test
97      public void testEscapeJavaWithSlash() {
98          final String input = "String with a slash (/) in it";
99  
100         final String expected = input;
101         final String actual = StringEscapeUtils.escapeJava(input);
102 
103         /**
104          * In 2.4 StringEscapeUtils.escapeJava(String) escapes '/' characters, which are not a valid character to escape
105          * in a Java string.
106          */
107         assertEquals(expected, actual);
108     }
109     
110     private void assertEscapeJava(final String escaped, final String original) throws IOException {
111         assertEscapeJava(null, escaped, original);
112     }
113 
114     private void assertEscapeJava(String message, final String expected, final String original) throws IOException {
115         final String converted = StringEscapeUtils.escapeJava(original);
116         message = "escapeJava(String) failed" + (message == null ? "" : (": " + message));
117         assertEquals(message, expected, converted);
118 
119         final StringWriter writer = new StringWriter();
120         StringEscapeUtils.ESCAPE_JAVA.translate(original, writer);
121         assertEquals(expected, writer.toString());
122     }
123 
124     @Test
125     public void testUnescapeJava() throws IOException {
126         assertEquals(null, StringEscapeUtils.unescapeJava(null));
127         try {
128             StringEscapeUtils.UNESCAPE_JAVA.translate(null, null);
129             fail();
130         } catch (final IOException ex) {
131             fail();
132         } catch (final IllegalArgumentException ex) {
133         }
134         try {
135             StringEscapeUtils.UNESCAPE_JAVA.translate("", null);
136             fail();
137         } catch (final IOException ex) {
138             fail();
139         } catch (final IllegalArgumentException ex) {
140         }
141         try {
142             StringEscapeUtils.unescapeJava("\\u02-3");
143             fail();
144         } catch (final RuntimeException ex) {
145         }
146         
147         assertUnescapeJava("", "");
148         assertUnescapeJava("test", "test");
149         assertUnescapeJava("\ntest\b", "\\ntest\\b");
150         assertUnescapeJava("\u123425foo\ntest\b", "\\u123425foo\\ntest\\b");
151         assertUnescapeJava("'\foo\teste\r", "\\'\\foo\\teste\\r");
152         assertUnescapeJava("", "\\");
153         //foo
154         assertUnescapeJava("lowercase Unicode", "\uABCDx", "\\uabcdx");
155         assertUnescapeJava("uppercase Unicode", "\uABCDx", "\\uABCDx");
156         assertUnescapeJava("Unicode as final character", "\uABCD", "\\uabcd");
157     }
158 
159     private void assertUnescapeJava(final String unescaped, final String original) throws IOException {
160         assertUnescapeJava(null, unescaped, original);
161     }
162 
163     private void assertUnescapeJava(final String message, final String unescaped, final String original) throws IOException {
164         final String expected = unescaped;
165         final String actual = StringEscapeUtils.unescapeJava(original);
166 
167         assertEquals("unescape(String) failed" +
168                 (message == null ? "" : (": " + message)) +
169                 ": expected '" + StringEscapeUtils.escapeJava(expected) +
170                 // we escape this so we can see it in the error message
171                 "' actual '" + StringEscapeUtils.escapeJava(actual) + "'",
172                 expected, actual);
173 
174         final StringWriter writer = new StringWriter();
175         StringEscapeUtils.UNESCAPE_JAVA.translate(original, writer);
176         assertEquals(unescaped, writer.toString());
177 
178     }
179 
180     @Test
181     public void testEscapeEcmaScript() {
182         assertEquals(null, StringEscapeUtils.escapeEcmaScript(null));
183         try {
184             StringEscapeUtils.ESCAPE_ECMASCRIPT.translate(null, null);
185             fail();
186         } catch (final IOException ex) {
187             fail();
188         } catch (final IllegalArgumentException ex) {
189         }
190         try {
191             StringEscapeUtils.ESCAPE_ECMASCRIPT.translate("", null);
192             fail();
193         } catch (final IOException ex) {
194             fail();
195         } catch (final IllegalArgumentException ex) {
196         }
197         
198         assertEquals("He didn\\'t say, \\\"stop!\\\"", StringEscapeUtils.escapeEcmaScript("He didn't say, \"stop!\""));
199         assertEquals("document.getElementById(\\\"test\\\").value = \\'<script>alert(\\'aaa\\');<\\/script>\\';", 
200                 StringEscapeUtils.escapeEcmaScript("document.getElementById(\"test\").value = '<script>alert('aaa');</script>';"));
201     }
202 
203 
204     // HTML and XML
205     //--------------------------------------------------------------
206 
207     private static final String[][] HTML_ESCAPES = {
208         {"no escaping", "plain text", "plain text"},
209         {"no escaping", "plain text", "plain text"},
210         {"empty string", "", ""},
211         {"null", null, null},
212         {"ampersand", "bread &amp; butter", "bread & butter"},
213         {"quotes", "&quot;bread&quot; &amp; butter", "\"bread\" & butter"},
214         {"final character only", "greater than &gt;", "greater than >"},
215         {"first character only", "&lt; less than", "< less than"},
216         {"apostrophe", "Huntington's chorea", "Huntington's chorea"},
217         {"languages", "English,Fran&ccedil;ais,\u65E5\u672C\u8A9E (nihongo)", "English,Fran\u00E7ais,\u65E5\u672C\u8A9E (nihongo)"},
218         {"8-bit ascii shouldn't number-escape", "\u0080\u009F", "\u0080\u009F"},
219     };
220 
221     @Test
222     public void testEscapeHtml() {
223         for (final String[] element : HTML_ESCAPES) {
224             final String message = element[0];
225             final String expected = element[1];
226             final String original = element[2];
227             assertEquals(message, expected, StringEscapeUtils.escapeHtml4(original));
228             final StringWriter sw = new StringWriter();
229             try {
230                 StringEscapeUtils.ESCAPE_HTML4.translate(original, sw);
231             } catch (final IOException e) {
232             }
233             final String actual = original == null ? null : sw.toString();
234             assertEquals(message, expected, actual);
235         }
236     }
237 
238     @Test
239     public void testUnescapeHtml4() {
240         for (final String[] element : HTML_ESCAPES) {
241             final String message = element[0];
242             final String expected = element[2];
243             final String original = element[1];
244             assertEquals(message, expected, StringEscapeUtils.unescapeHtml4(original));
245             
246             final StringWriter sw = new StringWriter();
247             try {
248                 StringEscapeUtils.UNESCAPE_HTML4.translate(original, sw);
249             } catch (final IOException e) {
250             }
251             final String actual = original == null ? null : sw.toString();
252             assertEquals(message, expected, actual);
253         }
254         // \u00E7 is a cedilla (c with wiggle under)
255         // note that the test string must be 7-bit-clean (Unicode escaped) or else it will compile incorrectly
256         // on some locales        
257         assertEquals("funny chars pass through OK", "Fran\u00E7ais", StringEscapeUtils.unescapeHtml4("Fran\u00E7ais"));
258         
259         assertEquals("Hello&;World", StringEscapeUtils.unescapeHtml4("Hello&;World"));
260         assertEquals("Hello&#;World", StringEscapeUtils.unescapeHtml4("Hello&#;World"));
261         assertEquals("Hello&# ;World", StringEscapeUtils.unescapeHtml4("Hello&# ;World"));
262         assertEquals("Hello&##;World", StringEscapeUtils.unescapeHtml4("Hello&##;World"));
263     }
264 
265     @Test
266     public void testUnescapeHexCharsHtml() {
267         // Simple easy to grok test 
268         assertEquals("hex number unescape", "\u0080\u009F", StringEscapeUtils.unescapeHtml4("&#x80;&#x9F;"));
269         assertEquals("hex number unescape", "\u0080\u009F", StringEscapeUtils.unescapeHtml4("&#X80;&#X9F;"));
270         // Test all Character values:
271         for (char i = Character.MIN_VALUE; i < Character.MAX_VALUE; i++) {
272             final Character c1 = new Character(i);
273             final Character c2 = new Character((char)(i+1));
274             final String expected = c1.toString() + c2.toString();
275             final String escapedC1 = "&#x" + Integer.toHexString((c1.charValue())) + ";";
276             final String escapedC2 = "&#x" + Integer.toHexString((c2.charValue())) + ";";
277             assertEquals("hex number unescape index " + (int)i, expected, StringEscapeUtils.unescapeHtml4(escapedC1 + escapedC2));
278         }
279     }
280 
281     @Test
282     public void testUnescapeUnknownEntity() throws Exception {
283         assertEquals("&zzzz;", StringEscapeUtils.unescapeHtml4("&zzzz;"));
284     }
285 
286     @Test
287     public void testEscapeHtmlVersions() throws Exception {
288         assertEquals("&Beta;", StringEscapeUtils.escapeHtml4("\u0392"));
289         assertEquals("\u0392", StringEscapeUtils.unescapeHtml4("&Beta;"));
290 
291         // TODO: refine API for escaping/unescaping specific HTML versions
292     }
293 
294     @Test
295     @SuppressWarnings( "deprecation" ) // ESCAPE_XML has been replaced by ESCAPE_XML10 and ESCAPE_XML11 in 3.3
296     public void testEscapeXml() throws Exception {
297         assertEquals("&lt;abc&gt;", StringEscapeUtils.escapeXml("<abc>"));
298         assertEquals("<abc>", StringEscapeUtils.unescapeXml("&lt;abc&gt;"));
299 
300         assertEquals("XML should not escape >0x7f values",
301                 "\u00A1", StringEscapeUtils.escapeXml("\u00A1"));
302         assertEquals("XML should be able to unescape >0x7f values",
303                 "\u00A0", StringEscapeUtils.unescapeXml("&#160;"));
304         assertEquals("XML should be able to unescape >0x7f values with one leading 0",
305                 "\u00A0", StringEscapeUtils.unescapeXml("&#0160;"));
306         assertEquals("XML should be able to unescape >0x7f values with two leading 0s",
307                 "\u00A0", StringEscapeUtils.unescapeXml("&#00160;"));
308         assertEquals("XML should be able to unescape >0x7f values with three leading 0s",
309                 "\u00A0", StringEscapeUtils.unescapeXml("&#000160;"));
310 
311         assertEquals("ain't", StringEscapeUtils.unescapeXml("ain&apos;t"));
312         assertEquals("ain&apos;t", StringEscapeUtils.escapeXml("ain't"));
313         assertEquals("", StringEscapeUtils.escapeXml(""));
314         assertEquals(null, StringEscapeUtils.escapeXml(null));
315         assertEquals(null, StringEscapeUtils.unescapeXml(null));
316 
317         StringWriter sw = new StringWriter();
318         try {
319             StringEscapeUtils.ESCAPE_XML.translate("<abc>", sw);
320         } catch (final IOException e) {
321         }
322         assertEquals("XML was escaped incorrectly", "&lt;abc&gt;", sw.toString() );
323 
324         sw = new StringWriter();
325         try {
326             StringEscapeUtils.UNESCAPE_XML.translate("&lt;abc&gt;", sw);
327         } catch (final IOException e) {
328         }
329         assertEquals("XML was unescaped incorrectly", "<abc>", sw.toString() );
330     }
331     
332     @Test
333     public void testEscapeXml10() throws Exception {
334         assertEquals("a&lt;b&gt;c&quot;d&apos;e&amp;f", StringEscapeUtils.escapeXml10("a<b>c\"d'e&f"));
335         assertEquals("XML 1.0 should not escape \t \n \r",
336                 "a\tb\rc\nd", StringEscapeUtils.escapeXml10("a\tb\rc\nd"));
337         assertEquals("XML 1.0 should omit most #x0-x8 | #xb | #xc | #xe-#x19",
338                 "ab", StringEscapeUtils.escapeXml10("a\u0000\u0001\u0008\u000b\u000c\u000e\u001fb"));
339         assertEquals("XML 1.0 should omit #xd800-#xdfff",
340                 "a\ud7ff  \ue000b", StringEscapeUtils.escapeXml10("a\ud7ff\ud800 \udfff \ue000b"));
341         assertEquals("XML 1.0 should omit #xfffe | #xffff",
342                 "a\ufffdb", StringEscapeUtils.escapeXml10("a\ufffd\ufffe\uffffb"));
343         assertEquals("XML 1.0 should escape #x7f-#x84 | #x86 - #x9f, for XML 1.1 compatibility",
344                 "a\u007e&#127;&#132;\u0085&#134;&#159;\u00a0b", StringEscapeUtils.escapeXml10("a\u007e\u007f\u0084\u0085\u0086\u009f\u00a0b"));
345     }
346     
347     @Test
348     public void testEscapeXml11() throws Exception {
349         assertEquals("a&lt;b&gt;c&quot;d&apos;e&amp;f", StringEscapeUtils.escapeXml11("a<b>c\"d'e&f"));
350         assertEquals("XML 1.1 should not escape \t \n \r",
351                 "a\tb\rc\nd", StringEscapeUtils.escapeXml11("a\tb\rc\nd"));
352         assertEquals("XML 1.1 should omit #x0",
353                 "ab", StringEscapeUtils.escapeXml11("a\u0000b"));
354         assertEquals("XML 1.1 should escape #x1-x8 | #xb | #xc | #xe-#x19",
355                 "a&#1;&#8;&#11;&#12;&#14;&#31;b", StringEscapeUtils.escapeXml11("a\u0001\u0008\u000b\u000c\u000e\u001fb"));
356         assertEquals("XML 1.1 should escape #x7F-#x84 | #x86-#x9F",
357                 "a\u007e&#127;&#132;\u0085&#134;&#159;\u00a0b", StringEscapeUtils.escapeXml11("a\u007e\u007f\u0084\u0085\u0086\u009f\u00a0b"));
358         assertEquals("XML 1.1 should omit #xd800-#xdfff",
359                 "a\ud7ff  \ue000b", StringEscapeUtils.escapeXml11("a\ud7ff\ud800 \udfff \ue000b"));
360         assertEquals("XML 1.1 should omit #xfffe | #xffff",
361                 "a\ufffdb", StringEscapeUtils.escapeXml11("a\ufffd\ufffe\uffffb"));
362     }
363 
364     /**
365      * Tests Supplementary characters. 
366      * <p>
367      * From http://www.w3.org/International/questions/qa-escapes
368      * </p>
369      * <blockquote>
370      * Supplementary characters are those Unicode characters that have code points higher than the characters in
371      * the Basic Multilingual Plane (BMP). In UTF-16 a supplementary character is encoded using two 16-bit surrogate code points from the
372      * BMP. Because of this, some people think that supplementary characters need to be represented using two escapes, but this is incorrect
373      * - you must use the single, code point value for that character. For example, use &amp;&#35;x233B4&#59; rather than
374      * &amp;&#35;xD84C&#59;&amp;&#35;xDFB4&#59;.
375      * </blockquote>
376      * @see <a href="http://www.w3.org/International/questions/qa-escapes">Using character escapes in markup and CSS</a>
377      * @see <a href="https://issues.apache.org/jira/browse/LANG-728">LANG-728</a>
378      */
379     @Test
380     @SuppressWarnings( "deprecation" ) // ESCAPE_XML has been replaced by ESCAPE_XML10 and ESCAPE_XML11 in 3.3
381     public void testEscapeXmlSupplementaryCharacters() {
382         final CharSequenceTranslator escapeXml = 
383             StringEscapeUtils.ESCAPE_XML.with( NumericEntityEscaper.between(0x7f, Integer.MAX_VALUE) );
384 
385         assertEquals("Supplementary character must be represented using a single escape", "&#144308;",
386                 escapeXml.translate("\uD84C\uDFB4"));
387 
388         assertEquals("Supplementary characters mixed with basic characters should be encoded correctly", "a b c &#144308;",
389                         escapeXml.translate("a b c \uD84C\uDFB4"));
390     }
391     
392     @Test
393     @SuppressWarnings( "deprecation" ) // ESCAPE_XML has been replaced by ESCAPE_XML10 and ESCAPE_XML11 in 3.3
394     public void testEscapeXmlAllCharacters() {
395         // http://www.w3.org/TR/xml/#charsets says:
396         // Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF] /* any Unicode character,
397         // excluding the surrogate blocks, FFFE, and FFFF. */
398         final CharSequenceTranslator escapeXml = StringEscapeUtils.ESCAPE_XML
399                 .with(NumericEntityEscaper.below(9), NumericEntityEscaper.between(0xB, 0xC), NumericEntityEscaper.between(0xE, 0x19),
400                         NumericEntityEscaper.between(0xD800, 0xDFFF), NumericEntityEscaper.between(0xFFFE, 0xFFFF), NumericEntityEscaper.above(0x110000));
401 
402         assertEquals("&#0;&#1;&#2;&#3;&#4;&#5;&#6;&#7;&#8;", escapeXml.translate("\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\u0008"));
403         assertEquals("\t", escapeXml.translate("\t")); // 0x9
404         assertEquals("\n", escapeXml.translate("\n")); // 0xA
405         assertEquals("&#11;&#12;", escapeXml.translate("\u000B\u000C"));
406         assertEquals("\r", escapeXml.translate("\r")); // 0xD
407         assertEquals("Hello World! Ain&apos;t this great?", escapeXml.translate("Hello World! Ain't this great?"));
408         assertEquals("&#14;&#15;&#24;&#25;", escapeXml.translate("\u000E\u000F\u0018\u0019"));
409     }
410     
411     /**
412      * Reverse of the above.
413      *
414      * @see <a href="https://issues.apache.org/jira/browse/LANG-729">LANG-729</a>
415      */
416     @Test
417     public void testUnescapeXmlSupplementaryCharacters() {
418         assertEquals("Supplementary character must be represented using a single escape", "\uD84C\uDFB4",
419                 StringEscapeUtils.unescapeXml("&#144308;") );
420 
421         assertEquals("Supplementary characters mixed with basic characters should be decoded correctly", "a b c \uD84C\uDFB4",
422                 StringEscapeUtils.unescapeXml("a b c &#144308;") );
423     }
424         
425     // Tests issue #38569
426     // http://issues.apache.org/bugzilla/show_bug.cgi?id=38569
427     @Test
428     public void testStandaloneAmphersand() {
429         assertEquals("<P&O>", StringEscapeUtils.unescapeHtml4("&lt;P&O&gt;"));
430         assertEquals("test & <", StringEscapeUtils.unescapeHtml4("test & &lt;"));
431         assertEquals("<P&O>", StringEscapeUtils.unescapeXml("&lt;P&O&gt;"));
432         assertEquals("test & <", StringEscapeUtils.unescapeXml("test & &lt;"));
433     }
434 
435     @Test
436     public void testLang313() {
437         assertEquals("& &", StringEscapeUtils.unescapeHtml4("& &amp;"));
438     }
439 
440     @Test
441     public void testEscapeCsvString() throws Exception {
442         assertEquals("foo.bar",            StringEscapeUtils.escapeCsv("foo.bar"));
443         assertEquals("\"foo,bar\"",        StringEscapeUtils.escapeCsv("foo,bar"));
444         assertEquals("\"foo\nbar\"",       StringEscapeUtils.escapeCsv("foo\nbar"));
445         assertEquals("\"foo\rbar\"",       StringEscapeUtils.escapeCsv("foo\rbar"));
446         assertEquals("\"foo\"\"bar\"",     StringEscapeUtils.escapeCsv("foo\"bar"));
447         assertEquals("foo\uD84C\uDFB4bar", StringEscapeUtils.escapeCsv("foo\uD84C\uDFB4bar"));
448         assertEquals("",   StringEscapeUtils.escapeCsv(""));
449         assertEquals(null, StringEscapeUtils.escapeCsv(null));
450     }
451 
452     @Test
453     public void testEscapeCsvWriter() throws Exception {
454         checkCsvEscapeWriter("foo.bar",            "foo.bar");
455         checkCsvEscapeWriter("\"foo,bar\"",        "foo,bar");
456         checkCsvEscapeWriter("\"foo\nbar\"",       "foo\nbar");
457         checkCsvEscapeWriter("\"foo\rbar\"",       "foo\rbar");
458         checkCsvEscapeWriter("\"foo\"\"bar\"",     "foo\"bar");
459         checkCsvEscapeWriter("foo\uD84C\uDFB4bar", "foo\uD84C\uDFB4bar");
460         checkCsvEscapeWriter("", null);
461         checkCsvEscapeWriter("", "");
462     }
463 
464     private void checkCsvEscapeWriter(final String expected, final String value) {
465         try {
466             final StringWriter writer = new StringWriter();
467             StringEscapeUtils.ESCAPE_CSV.translate(value, writer);
468             assertEquals(expected, writer.toString());
469         } catch (final IOException e) {
470             fail("Threw: " + e);
471         }
472     }
473 
474     @Test
475     public void testUnescapeCsvString() throws Exception {
476         assertEquals("foo.bar",              StringEscapeUtils.unescapeCsv("foo.bar"));
477         assertEquals("foo,bar",              StringEscapeUtils.unescapeCsv("\"foo,bar\""));
478         assertEquals("foo\nbar",             StringEscapeUtils.unescapeCsv("\"foo\nbar\""));
479         assertEquals("foo\rbar",             StringEscapeUtils.unescapeCsv("\"foo\rbar\""));
480         assertEquals("foo\"bar",             StringEscapeUtils.unescapeCsv("\"foo\"\"bar\""));
481         assertEquals("foo\uD84C\uDFB4bar",   StringEscapeUtils.unescapeCsv("foo\uD84C\uDFB4bar"));
482         assertEquals("",   StringEscapeUtils.unescapeCsv(""));
483         assertEquals(null, StringEscapeUtils.unescapeCsv(null));
484 
485         assertEquals("\"foo.bar\"",          StringEscapeUtils.unescapeCsv("\"foo.bar\""));
486     }
487 
488     @Test
489     public void testUnescapeCsvWriter() throws Exception {
490         checkCsvUnescapeWriter("foo.bar",            "foo.bar");
491         checkCsvUnescapeWriter("foo,bar",            "\"foo,bar\"");
492         checkCsvUnescapeWriter("foo\nbar",           "\"foo\nbar\"");
493         checkCsvUnescapeWriter("foo\rbar",           "\"foo\rbar\"");
494         checkCsvUnescapeWriter("foo\"bar",           "\"foo\"\"bar\"");
495         checkCsvUnescapeWriter("foo\uD84C\uDFB4bar", "foo\uD84C\uDFB4bar");
496         checkCsvUnescapeWriter("", null);
497         checkCsvUnescapeWriter("", "");
498 
499         checkCsvUnescapeWriter("\"foo.bar\"",        "\"foo.bar\"");
500     }
501 
502     private void checkCsvUnescapeWriter(final String expected, final String value) {
503         try {
504             final StringWriter writer = new StringWriter();
505             StringEscapeUtils.UNESCAPE_CSV.translate(value, writer);
506             assertEquals(expected, writer.toString());
507         } catch (final IOException e) {
508             fail("Threw: " + e);
509         }
510     }
511 
512     /**
513      * Tests // https://issues.apache.org/jira/browse/LANG-480
514      */
515     @Test
516     public void testEscapeHtmlHighUnicode() {
517         // this is the utf8 representation of the character:
518         // COUNTING ROD UNIT DIGIT THREE
519         // in Unicode
520         // codepoint: U+1D362
521         final byte[] data = new byte[] { (byte)0xF0, (byte)0x9D, (byte)0x8D, (byte)0xA2 };
522 
523         final String original = new String(data, Charset.forName("UTF8"));
524 
525         final String escaped = StringEscapeUtils.escapeHtml4( original );
526         assertEquals( "High Unicode should not have been escaped", original, escaped);
527 
528         final String unescaped = StringEscapeUtils.unescapeHtml4( escaped );
529         assertEquals( "High Unicode should have been unchanged", original, unescaped);
530 
531 // TODO: I think this should hold, needs further investigation
532 //        String unescapedFromEntity = StringEscapeUtils.unescapeHtml4( "&#119650;" );
533 //        assertEquals( "High Unicode should have been unescaped", original, unescapedFromEntity);
534     }
535 
536     /**
537      * Tests https://issues.apache.org/jira/browse/LANG-339
538      */
539     @Test
540     public void testEscapeHiragana() {
541         // Some random Japanese Unicode characters
542         final String original = "\u304B\u304C\u3068";
543         final String escaped = StringEscapeUtils.escapeHtml4(original);
544         assertEquals( "Hiragana character Unicode behaviour should not be being escaped by escapeHtml4",
545         original, escaped);
546 
547         final String unescaped = StringEscapeUtils.unescapeHtml4( escaped );
548 
549         assertEquals( "Hiragana character Unicode behaviour has changed - expected no unescaping", escaped, unescaped);
550     }
551 
552     /**
553      * Tests https://issues.apache.org/jira/browse/LANG-708
554      * 
555      * @throws IOException
556      *             if an I/O error occurs
557      */
558     @Test
559     public void testLang708() throws IOException {
560         final FileInputStream fis = new FileInputStream("src/test/resources/lang-708-input.txt");
561         final String input = IOUtils.toString(fis, "UTF-8");
562         final String escaped = StringEscapeUtils.escapeEcmaScript(input);
563         // just the end:
564         assertTrue(escaped, escaped.endsWith("}]"));
565         // a little more:
566         assertTrue(escaped, escaped.endsWith("\"valueCode\\\":\\\"\\\"}]"));
567         fis.close();
568     }
569 
570     /**
571      * Tests https://issues.apache.org/jira/browse/LANG-720
572      */
573     @Test
574     @SuppressWarnings( "deprecation" ) // escapeXml(String) has been replaced by escapeXml10(String) and escapeXml11(String) in 3.3
575     public void testLang720() {
576         final String input = new StringBuilder("\ud842\udfb7").append("A").toString();
577         final String escaped = StringEscapeUtils.escapeXml(input);
578         assertEquals(input, escaped);
579     }
580 
581     /**
582      * Tests https://issues.apache.org/jira/browse/LANG-911
583      */
584     @Test
585     public void testLang911() {
586         final String bellsTest = "\ud83d\udc80\ud83d\udd14";
587         final String value = StringEscapeUtils.escapeJava(bellsTest);
588         final String valueTest = StringEscapeUtils.unescapeJava(value);
589         assertEquals(bellsTest, valueTest);
590     }
591 
592     @Test
593     public void testEscapeJson() {
594         assertEquals(null, StringEscapeUtils.escapeJson(null));
595         try {
596             StringEscapeUtils.ESCAPE_JSON.translate(null, null);
597             fail();
598         } catch (final IOException ex) {
599             fail();
600         } catch (final IllegalArgumentException ex) {
601         }
602         try {
603             StringEscapeUtils.ESCAPE_JSON.translate("", null);
604             fail();
605         } catch (final IOException ex) {
606             fail();
607         } catch (final IllegalArgumentException ex) {
608         }
609 
610         assertEquals("He didn't say, \\\"stop!\\\"", StringEscapeUtils.escapeJson("He didn't say, \"stop!\""));
611 
612         final String expected = "\\\"foo\\\" isn't \\\"bar\\\". specials: \\b\\r\\n\\f\\t\\\\\\/";
613         final String input ="\"foo\" isn't \"bar\". specials: \b\r\n\f\t\\/";
614 
615         assertEquals(expected, StringEscapeUtils.escapeJson(input));
616     }
617 
618 }